package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsboolDataOnDemand.
 */
@Configurable
@Component
@RooDataOnDemand(entity = Ttlsbool.class)
public class TtlsboolDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbool> data;

	/**
	 * Gets the new transient ttlsbool.
	 *
	 * @param index the index
	 * @return the new transient ttlsbool
	 */
	public Ttlsbool getNewTransientTtlsbool(int index) {
        Ttlsbool obj = new Ttlsbool();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodBancoint(obj, index);
        setCodCanaldi(obj, index);
        setCodLogonapl(obj, index);
        setCodTipord(obj, index);
        setDesOrdenol(obj, index);
        setXsnOrdeneje(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbool obj, int index) {
        TtlsboolPK embeddedIdClass = new TtlsboolPK(BigDecimal.valueOf(index), new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime(), new Integer(index).shortValue());
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbool obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbool obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod bancoint.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodBancoint(Ttlsbool obj, int index) {
        Short codBancoint = new Integer(index).shortValue();
        obj.setCodBancoint(codBancoint);
    }

	/**
	 * Sets the cod canaldi.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodCanaldi(Ttlsbool obj, int index) {
        Short codCanaldi = new Integer(index).shortValue();
        obj.setCodCanaldi(codCanaldi);
    }

	/**
	 * Sets the cod logonapl.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodLogonapl(Ttlsbool obj, int index) {
        String codLogonapl = "codLogonapl_" + index;
        if (codLogonapl.length() > 20) {
            codLogonapl = codLogonapl.substring(0, 20);
        }
        obj.setCodLogonapl(codLogonapl);
    }

	/**
	 * Sets the cod tipord.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodTipord(Ttlsbool obj, int index) {
        String codTipord = "co_" + index;
        if (codTipord.length() > 4) {
            codTipord = codTipord.substring(0, 4);
        }
        obj.setCodTipord(codTipord);
    }

	/**
	 * Sets the des ordenol.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesOrdenol(Ttlsbool obj, int index) {
        String desOrdenol = "desOrdenol_" + index;
        if (desOrdenol.length() > 500) {
            desOrdenol = desOrdenol.substring(0, 500);
        }
        obj.setDesOrdenol(desOrdenol);
    }

	/**
	 * Sets the xsn ordeneje.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnOrdeneje(Ttlsbool obj, int index) {
        Character xsnOrdeneje = new Character('N');
        obj.setXsnOrdeneje(xsnOrdeneje);
    }

	/**
	 * Gets the specific ttlsbool.
	 *
	 * @param index the index
	 * @return the specific ttlsbool
	 */
	public Ttlsbool getSpecificTtlsbool(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbool obj = data.get(index);
        return Ttlsbool.findTtlsbool(obj.getId());
    }

	/**
	 * Gets the random ttlsbool.
	 *
	 * @return the random ttlsbool
	 */
	public Ttlsbool getRandomTtlsbool() {
        init();
        Ttlsbool obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbool.findTtlsbool(obj.getId());
    }

	/**
	 * Modify ttlsbool.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbool(Ttlsbool obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbool.findTtlsboolEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbool' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Ttlsbool>();
        for (int i = 0; i < 10; i++) {
            Ttlsbool obj = getNewTransientTtlsbool(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
